<%@ page import="com.example.util.DBUtil" %>
<%@ page import="com.example.domain.User" %>
<%@ page import="java.sql.*" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>简单购物车测试</title>
</head>
<body>
    <h1>简单购物车测试</h1>
    
    <%
        User currentUser = (User) session.getAttribute("user");
        if (currentUser == null) {
    %>
        <p>请先<a href="../auth/login.jsp">登录</a></p>
    <%
        } else {
    %>
        <p>当前用户: <%= currentUser.getUsername() %> (ID: <%= currentUser.getId() %>)</p>
        
        <h2>添加商品到购物车</h2>
        <form method="post">
            商品ID: <input type="number" name="productId" required><br><br>
            数量: <input type="number" name="quantity" value="1" required><br><br>
            <input type="submit" value="添加到购物车">
        </form>
        
        <%
            if ("POST".equalsIgnoreCase(request.getMethod())) {
                try {
                    int productId = Integer.parseInt(request.getParameter("productId"));
                    int quantity = Integer.parseInt(request.getParameter("quantity"));
                    
                    // 直接调用add_to_cart.jsp
                    response.sendRedirect("../cart/api/add.jsp?productId=" + productId + "&quantity=" + quantity);
                    return;
                } catch (Exception e) {
                    out.println("<p style='color: red;'>错误: " + e.getMessage() + "</p>");
                }
            }
        %>
        
        <h2>检查结果</h2>
        <a href="view_cart.jsp">查看购物车</a>
    <%
        }
    %>
</body>
</html>